package com.example.fpe_vote.db1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.fpe_vote.common.Result;
import com.example.fpe_vote.db1.entity.NomineeInfo;
import com.example.fpe_vote.db1.entity.PrizeInfo;
import com.example.fpe_vote.db1.entity.VoteInfo;
import com.example.fpe_vote.db1.service.INomineeInfoService;
import com.example.fpe_vote.db1.service.IPrizeInfoService;
import com.example.fpe_vote.db1.service.IVoteInfoService;
import com.example.fpe_vote.db2.entity.Ygzlw;
import com.example.fpe_vote.db2.service.IYgzlwService;
import com.example.fpe_vote.utils.MyStringUtil;
import com.example.fpe_vote.vo.request.RequestAddVoteVo;
import com.example.fpe_vote.vo.request.RequestListVoteInfoVo;
import com.example.fpe_vote.vo.response.ResponseListVoteInfoVo;
import com.example.fpe_vote.vo.response.ResponseVoteResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/vote-info")
public class VoteInfoController {
    @Autowired
    IVoteInfoService voteInfoService;
    @Autowired
    IPrizeInfoService prizeInfoService;
    @Autowired
    INomineeInfoService nomineeInfoService;
    @Autowired
    IYgzlwService ygzlwService;

    //投票接口
    @PostMapping("/addVote")
    public Result addVote(@RequestBody RequestAddVoteVo voteVo){
        //判断用户正确性
        Ygzlw ygzlw=ygzlwService.queryByInfo(voteVo.getUserName(),voteVo.getGhid(),voteVo.getSfz());
        if(ObjectUtils.isEmpty(ygzlw)){
            return Result.getFailure().setMsg("投票失败！无效的用户信息！");
        }
        //判断是否投过票
        if(ObjectUtils.isNotEmpty(voteInfoService.queryOne(voteVo.getUserName(),voteVo.getGhid() ,voteVo.getPrizeNum()))){
            return Result.getFailure().setMsg("投票失败！该奖项您已经投过票了！左上角可查看投票记录");
        }
        //判断是否提交过多选择
        PrizeInfo prizeInfo=prizeInfoService.queryByNum(voteVo.getPrizeNum());
        if(voteVo.getVoteNomineeIds().length>prizeInfo.getAllowAmounts()){
            return Result.getFailure().setMsg("投票失败！只允许选择"+prizeInfo.getAllowAmounts()+"个!");
        }
        //投票操作进行
        if(voteInfoService.addVote(voteVo)){
            return Result.getSuccess().setMsg("投票成功！");
        }else
            return Result.getFailure().setMsg("投票失败...请重新尝试！");
    }

    //查询投票记录
    @PostMapping("/listVoteInfo")
    public Result listVoteInfo(@RequestBody RequestListVoteInfoVo voteInfoVo){
        IPage<ResponseListVoteInfoVo> iPage=voteInfoService.listVoteInfo(voteInfoVo);
        return Result.getSuccess().setData(iPage);
    }


}
